
import env from "./env"
import { setUserInfo, clearUserInfo } from './store';

//Request类
class Request {
    //构造
    constructor(params) {
        this.withBaseURL = params.withBaseURL
        this.baseURL = env.baseUrl
    }
    //get请求
    get(url, data) {
        return this.request('GET', url, data)
    }
    //post请求
    post(url, data) {
        return this.request('POST', url, data)
    }
    //put请求
    put(url, data) {
        return this.request('PUT', url, data)
    }
    //delete请求
    delete(url, data) {
        return this.request('DELETE', url, data)
    }
    //发送请求
    request(method, url, data) {
        const vm = this
        // 封装请求头
        const newUrl = vm.withBaseURL ? vm.baseURL + url : url;
        const staffId = wx.getStorageSync('userinfo').staffId||'';
        const sapId = wx.getStorageSync('userinfo').siocSapNo||''
        //request 封装header token

        const header = {
            //设置参数内容类型为x-www-form-urlencoded
            // 'content-type': 'application/x-www-form-urlencoded',
            // 'Accept': 'application/json',
        }

        data = Object.assign({}, data, { staffId },{ sapId }) //20014153 我的工号
        wx.showLoading({
            title: '加载中...',
            mask: true, // 是否显示透明蒙层，防止触摸穿透  
        })

        console.log("请求接口",newUrl)
        console.log("请求入参",data)


        //封装Promise调用
        return new Promise((resolve, reject) => {
            wx.request({
                url: newUrl,
                data,
                method,
                header,
                async success(res) {
                    console.log('请求返回结果',res)
                    const { data: { code, msg } } = res;
                    const pages = getCurrentPages()
                    //登录失效
                    if(code==401){
                        const currentPage = pages[pages.length-1];
                        console.log('currentPage',currentPage);
                        console.log('currentPage.options',currentPage.options);
                        //clearUserInfo()
                        wx.showModal({
                            title: '提示',
                            content: '请先登录',
                            success: function(res) {
                                wx.reLaunch({
                                    url: `/pages/login/index?url=${currentPage.route}&urlParams=${JSON.stringify(currentPage.options)}`,
                                  
                                })
                            }
                        })
                        return;
                    }

                    if (code > 200) {
                        wx.showModal({
                            title: '提示',
                            content: msg || '服务器发生错误',
                        })
                        return
                    }

                   
                    return resolve(res.data)

                },
                fail(err) {
                    reject({
                        msg: '请求失败',
                        url: newUrl,
                        method,
                        data,
                        err
                    })
                },
                complete(e) {

                    wx.hideLoading();

                }
            })
        })
    }
}
const api = new Request({
    withBaseURL: true,
})

module.exports = api